public class MyQueue3 {
    /**
     * 使用双链表实现队列
     * 头删，尾插操作
     */

    static class ListNode {
        public int val;
        public ListNode next;
        public ListNode prev;
        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;
    /**
     * 入队操作，尾插
     * @param
     */
    public void offer(int val) {
        ListNode node = new ListNode(val);
        if(head == null) {
            head = node;
            last = node;
            return;
        }
        last.next = node;
        node.prev = last;
        last = node;
    }

    /**
     * 出队
     * @return
     */
    public int pool() {
        if(head == null) {
            System.out.print("队列为空: ");
            return -1;
        }
        if(head.next == null) {
            int tmp = head.val;
            last = null;
            head = null;
            return tmp;
        }
        int tmp = head.val;
        head = head.next;
        return tmp;
    }

    /**
     * 查看队头元素
     * @return
     */
    public int peek() {
        if(head == null) {
            System.out.print("队列为空");
            return -1;
        }
        int tmp = head.val;
        return tmp;
    }
    public boolean isEmpty() {
        return head == null;
    }
    public int size() {
        ListNode cur = head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

}
